package com.berwin.caper.entry;

import com.berwin.caper.sftp.CaperSFTPTransfer;
import com.berwin.caper.view.MainView;
import com.jcraft.jsch.*;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;
import java.util.Vector;

public class CaperSFTP {

    public static String jumpHost = "123.56.148.48";
    public static int jumpPort = 60022;
    public static String jumpUserName = "tangbowen";
    public static String jumpPassword = "znGH*T!BA4&T";

    public static String remoteHost = "172.17.16.29";
    public static int remotePort = 12358;


    public static String localReferenceHost = "127.0.0.1";
    public static int localReferencePort = remotePort;

    public static void main(String[] args) {
        new MainView().setVisible(true);
    }

//    public static void main(String[] args) throws Exception {
////        new MainView().setVisible(true);
////        CaperSFTP.test();
//        CaperSFTPTransfer caper = new CaperSFTPTransfer(jumpHost, jumpPort, jumpUserName, jumpPassword, remoteHost, remotePort);
//        caper.connect();
//        caper.jump("/sftp_root@Test-BUYU(vpc-2ze8rcbunydqzw7vatezk)_172.17.16.29:12358");
////        caper.cd("/home/game");
////        caper.getRemoteSftpChannel().mkdir("test");
////        caper.upload("/home/game/billiards/test", "cd_metro.jpg", "/Users/berwin/Downloads/cd_metro.jpg");
//        caper.uploadFolder("/home/game/billiards/test", "/Users/berwin/Downloads/test");
////        Vector jumpList = caper.getRemoteSftpChannel().ls("./");
////        for (Object file : jumpList) {
////            System.out.println("jump ls:" + file.toString());
////        }
//        System.out.println("完成");
//    }

    public static void test() {
        Session jumpSession = null;
        Session remoteSession = null;
        ChannelSftp sftpChannel = null;
        try {
            JSch jSch = new JSch();
            jumpSession = jSch.getSession(jumpUserName, jumpHost, jumpPort);
            jumpSession.setPassword(jumpPassword);
            Properties sshConfig = new Properties();
            sshConfig.put("StrictHostKeyChecking", "no");
            sshConfig.put("PreferredAuthentications", "password,keyboard-interactive");
            jumpSession.setConfig(sshConfig);
            jumpSession.connect(1000 * 30);
            ChannelSftp jumpSFTP = (ChannelSftp) jumpSession.openChannel("sftp");
            jumpSFTP.connect(1000 * 30);
            jumpSFTP.cd("/sftp_root@Test-BUYU(vpc-2ze8rcbunydqzw7vatezk)_172.17.16.29:12358");
            jumpSFTP.cd("/home/game/");
            Vector jumpList = jumpSFTP.ls("./");
            for (Object file : jumpList) {
                System.out.println("jump ls:" + file.toString());
            }
            // 39.97.239.15 root/0w*$RvlfE6tLHNDS
//            int assignedPort = jumpSession.setPortForwardingL(localReferencePort, remoteHost, remotePort); // 此处开始为端口映射到本地的部分
//            System.out.println("assignedPort:" + assignedPort);
//
//            // 完成上诉映射之后，即可通过本地端口连接了
//            remoteSession = jSch.getSession("root", localReferenceHost, assignedPort);
//            remoteSession.setPassword("0w*$RvlfE6tLHNDS");
//            Properties remoteCfg = new Properties();
//            remoteCfg.put("StrictHostKeyChecking", "no");
//            remoteCfg.put("PreferredAuthentications", "password,keyboard-interactive");
//            remoteSession.setConfig(remoteCfg);
//            remoteSession.connect(1000 * 30);
//            System.out.println(remoteSession.getServerVersion());
//            // 创建SFTP隧道
//            Channel channel = remoteSession.openChannel("sftp"); // 创建sftp通信通道
//            sftpChannel = (ChannelSftp) channel;
//            sftpChannel.connect();
//            Vector list = sftpChannel.ls("/home/game/billiards");
//            for (Object file : list) {
//                System.out.println("ls:" + file.toString());
//            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
//                jumpSession.delPortForwardingL(localReferenceHost, localReferencePort);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (jumpSession != null && jumpSession.isConnected())
                jumpSession.disconnect();
            if (remoteSession != null && remoteSession.isConnected())
                remoteSession.disconnect();
            if (sftpChannel != null && remoteSession.isConnected())
                sftpChannel.disconnect();

        }
    }


}
